﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Core.Base;
using System.Runtime.InteropServices;
using System.Drawing;
using System.ComponentModel;
using AnimatorNS;
using Core.Component;
using System.Xml.Serialization;
using System.IO;
using Core;

namespace Components
{
    public class SGVScreen : Form, ISGVControl
    {
        public BaseModule module;
        public Animator animator;
        private IContainer components;
        public BaseEntity entity;

        public SGVScreen()
            : base()
        {
            InitializeComponent();
            animator.MaxAnimationTime = 250;
            this.ShowIcon = false;
        }

        public Core.Component.UIComponent component
        {
            set;
            get;
        }

        public void InitContextMenu()
        {
            this.ContextMenuStrip = new ContextMenuStrip();
            ToolStripMenuItem mnuExportLayout = new ToolStripMenuItem();
            mnuExportLayout.Text = "Export Layout";
            mnuExportLayout.Image = AppUtil.GetImageFromFileName(Application.StartupPath + @"/Images", "Export", 16, 16);
            mnuExportLayout.ImageAlign = ContentAlignment.MiddleLeft;
            this.ContextMenuStrip.Items.Add(mnuExportLayout);
            mnuExportLayout.Click += new EventHandler(mnuExportLayout_Click);
        }

        void mnuExportLayout_Click(object sender, EventArgs e)
        {
            SaveFileDialog saveDlg = new SaveFileDialog();
            saveDlg.ShowDialog();

            String strFileName = saveDlg.FileName;

            StreamWriter fStream = new StreamWriter(strFileName);

            UIComponent component = this.Serialize();
            component.Location = new Point(0, 0);
            XmlSerializer serializer = new XmlSerializer(typeof(UIComponent));
            serializer.Serialize(fStream, component);

            fStream.Close();
        }

        #region ISGV Control
        String _sGVShortcutString;
        [CategoryAttribute(".SGV"), DescriptionAttribute("")]
        public String SGVShortcutString
        {
            get { return _sGVShortcutString; }
            set 
            { 
                _sGVShortcutString = value;
                if (String.IsNullOrEmpty(_sGVShortcutString))
                {
                    //statusStrip1.Items.Clear();
                    //statusStrip1.Visible = false;
                }
                else
                {
                    String[] lstStrs = _sGVShortcutString.Split('|');
                    this.SuspendLayout();
                    foreach (String str in lstStrs)
                    {
                        ToolStripStatusLabel item = new ToolStripStatusLabel();
                        item.AutoSize = true;
                        item.Text = str;
                       // statusStrip1.Items.Add(item);
                    }
                    //statusStrip1.Visible = true;
                    this.ResumeLayout();
                }
            }
        }
        String _sGVTag;
        [CategoryAttribute(".SGV"), DescriptionAttribute("")]
        public string SGVTag
        {
            get
            {
                return _sGVTag;
            }
            set
            {
                _sGVTag = value;
                
            }
        } 
        #endregion

        public virtual void RegisterModule(BaseModule _module)
        {
            if (_module != null)
            {
                this.module = _module;
                this.entity = _module.entity;
            }
        }

        public void Init()
        {
            //AutoScroll = true;
            InitContextMenu();
            this.Font = new Font("Segoe UI", this.Font.Size, this.Font.Style);
        }

        public virtual void CloseScreen()
        {
            this.Close();
        }


        public void UpdateFormat()
        {
        }

        private void InitializeComponent()
        {
            this.components = new System.ComponentModel.Container();
            AnimatorNS.Animation animation3 = new AnimatorNS.Animation();
            System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(SGVScreen));
            this.animator = new AnimatorNS.Animator(this.components);
            this.SuspendLayout();
            // 
            // animator
            // 
            this.animator.AnimationType = AnimatorNS.AnimationType.Custom;
            this.animator.Cursor = null;
            animation3.AnimateOnlyDifferences = true;
            animation3.BlindCoeff = ((System.Drawing.PointF)(resources.GetObject("animation3.BlindCoeff")));
            animation3.LeafCoeff = 0F;
            animation3.MaxTime = 1F;
            animation3.MinTime = 0F;
            animation3.MosaicCoeff = ((System.Drawing.PointF)(resources.GetObject("animation3.MosaicCoeff")));
            animation3.MosaicShift = ((System.Drawing.PointF)(resources.GetObject("animation3.MosaicShift")));
            animation3.MosaicSize = 0;
            animation3.Padding = new System.Windows.Forms.Padding(0);
            animation3.RotateCoeff = 0F;
            animation3.RotateLimit = 0F;
            animation3.ScaleCoeff = ((System.Drawing.PointF)(resources.GetObject("animation3.ScaleCoeff")));
            animation3.SlideCoeff = ((System.Drawing.PointF)(resources.GetObject("animation3.SlideCoeff")));
            animation3.TimeCoeff = 0F;
            animation3.TransparencyCoeff = 0F;
            this.animator.DefaultAnimation = animation3;
            // 
            // SGVScreen
            // 
            this.ClientSize = new System.Drawing.Size(387, 355);
            this.animator.SetDecoration(this, AnimatorNS.DecorationType.None);
            this.Name = "SGVScreen";
            this.ResumeLayout(false);

        }

        protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
        {
            if (keyData == (Keys.Control | Keys.E))
            {
                mnuExportLayout_Click(null, new EventArgs());
            }
            return base.ProcessCmdKey(ref msg, keyData);
        }

        public UIComponent Serialize()
        {
            UIComponent component = new UIComponent();
            SGVControlHelper.CreateUIComponentFromControl(component, this);

            if (component != null)
            {
                component.Value = this.Text;

                foreach (Control ChildControl in this.Controls)
                {
                    if (ChildControl is ISGVControl)
                    {
                        UIComponent childComponent = ((ISGVControl)ChildControl).Serialize();
                        if (childComponent != null)
                            component.lstChilds.Add(childComponent);
                    }
                }
            }
            return component;
        }

        public void Deserialize(UIComponent component)
        {
        }
    }
}
